package org.jeecg.modules.system.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.jeecg.common.api.vo.Result;
import org.jeecg.modules.gwcjava.jiekou.entity.CategoryInfo;
import org.jeecg.modules.gwcjava.jiekou.service.BusinessLogicService;
import org.jeecg.modules.system.entity.AgreementEntity;
import org.jeecg.modules.system.service.AgreementService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/AgreementManagement")
public class AgreementController {

    @Autowired
    AgreementService service;

    /**
     * 查询所有协议
     * @param pageSize
     * @param pageNo
     * @return
     */
    @GetMapping("/list")
    public Result<?> AgreementList(
            @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
            @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
            String title,
            @RequestParam(name = "type", defaultValue = "10") int type
    ){
        Page<AgreementEntity> page=new  Page<AgreementEntity>(pageNo, pageSize);
        IPage<AgreementEntity> pageList=service.getAgreementList(page,title,type);
        return Result.ok(pageList);
    }


    /**
     * 新增协议
     */
    @PostMapping("/addAgreement")
    public Result<?> addAgreement(
            @RequestBody AgreementEntity entity
    ){
       try {
           if(entity.getType()==0){
               //查询是否已经存在
               String cunzai=service.getxieyicunzai(entity.getType());
               if(cunzai!=null){
                   return Result.ok("用户协议只允许存在一份，无法额外添加");
               }
           }

//           if(entity.getType()==1){
//               if(entity.getCategoryId()==null||"".equals(entity.getCategoryId()))
//                   return Result.ok("未选择品类,请选择");
//           }


           AgreementEntity   entitycunzai=service.getcanzhaobycateId(entity.getCategoryId());
           if(entitycunzai!=null){
               return Result.ok("同品类下只允许存在一份");
           }

           int count=service.addAgreement(entity);
           return Result.ok("新增成功");
       }catch (Exception e){
           return Result.error("新增出错");
       }
    }


    /**
     * 编辑修改协议
     */
    @PostMapping("/updateAgreement")
    public Result<?> updateAgreement(
            @RequestBody AgreementEntity entity
    ){
        try {
            AgreementEntity   entitycunzai=service.getcanzhaobycateId(entity.getCategoryId());
            if(entitycunzai!=null){
                if(entitycunzai.getId()!=entity.getId())
                    return Result.error("该品类已存在，请重新选择");
            }
            int count=service.updateAgreement(entity);
            return Result.ok("编辑成功");
        }catch (Exception e){
            e.printStackTrace();
            return Result.error("编辑出错");
        }
    }

    /**
     * 修改状态，发布，停用，删除通用
     */
    @GetMapping("/updatestatus")
    public Result<?> updatestatus(
            @RequestParam(name = "id") int id,
            @RequestParam(name = "status") int status
    ){
        try {
            int count=service.updatestatus(id,status);
            return Result.ok("操作成功");
        }catch (Exception e){
            return Result.error("修改出错");
        }

    }

    /**
     * 查询协议详情
     */
    @GetMapping("/AgreementDetail")
    public Result<?> AgreementDetail(
            @RequestParam(name = "id") int id
    ){
        AgreementEntity entity=service.getAgreementEntitybyId(id);
        return Result.ok(entity);
    }




}
